$(function () {
    //获取url
    var url = localStorage.getItem("ajaxUrl");
    var pass = 1;
    var focus = true;
    var deliverRope = JSON.parse(localStorage.getItem("deliverRope"));

    //点击返回按钮
    $(".head").on("click", ".goBack", function () {
        $.confirm("确定返回吗？", function () {
            loading_show("返回中...");
            $.ajax({
                headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
                type: "post",
                url: url + '/app/deliver/noSelectSaleoutorder',
                data: {
                    _id: deliverRope.id
                },
                dataType: 'json',
                json: 'callback',
                cache: false,
                timeout: 60000,
                success: function (res) {
                    loading_hide();
                    if (res.success) {
                        localStorage.removeItem("deliverRope");
                        window.location.href = "deliverRope_one.html";
                    } else {
                        $alert(res.message);
                    }
                },
                error: function (error) {
                    loading_hide();
                    if (error.status == 0) {
                        //loading_hide();
                        $alert("请求超时，请重新操作！");
                    } else {
                        //loading_hide();
                        $alert(error.responseText);
                    }
                }
            });
        }, function () {
            $("#goods_id").focus();
        });
    });

    //搜索框 获取焦点
    $("#goods_id").focus();
    $("#goods_id").blur(function () {
        if (focus) {
            $("#goods_id").focus();
        }
    });

    //点击 显示 隐藏
    $(".head .icon-xianshi").on("click", function () {
        $(this).hide();
        $(".head .icon-yincang").show();
        $(".details").show();
        //$alert("显示成功");
        setTimeout(function () {
            $("#goods_id").val("").focus();
        }, 1400);
    });
    $(".head .icon-yincang").on("click", function () {
        $(this).hide();
        $(".head .icon-xianshi").show();
        $(".details").hide();
        //$alert("隐藏成功");
        setTimeout(function () {
            $("#goods_id").val("").focus();
        }, 1400);
    });

    $(".details span.good_workplanNumber").html(deliverRope.houseName);
    $(".details span.good_gg").html(deliverRope.specification);
    $(".details span.good_zl").html(deliverRope.weight);
    var locationHtml = '';
    for (var i = 0; i < deliverRope.locationInventorys.length; i++) {
        locationHtml += '<p>指引库位:<span class="good_location">' +
            deliverRope.locationInventorys[i]._id.positionCode + '</span>件数:<span class="good_locationLength">' +
            deliverRope.locationInventorys[i].lengths + '</span>重量:<span class="good_locationWeight">' +
            deliverRope.locationInventorys[i].weights + '</span></p>'
    }
    $(".details").prepend(locationHtml);

    //获取 发货单 明细
    loading_show("获取中...");
    $.ajax({
        headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
        type: "post",
        url: url + '/app/deliver/getRopeSaleoutorderDetailsNew',
        data: {
            _id: deliverRope.id
        },
        dataType: 'json',
        json: 'callback',
        cache: false,
        timeout: 60000,
        success: function (res) {
            loading_hide();
            if (res.success) {
                var data = res.result;
                var data_html = '';
                for (var i = 0; i < data.length; i++) {
                    data_html += '<li attr-tjh="' + (data[i].palletBatch || "") + '" attr-data="' + data[i].batch + '"><div class="row no-gutter">' +
                        '<div class="col-90">' +
                        '<p></p><p><span>编号:</span><span class="sp goods_cpph">' +
                        data[i].batch + '</span></p>';
                    if (data[i].palletBatch) {
                        data_html += '<p><span>托架号:</span><span class="sp goods_cptjh">' + (data[i].palletBatch || "") + '</span></p>';
                    }
                    data_html += '<p><span>净重:</span><span class="sp goods_cpzl">' +
                        data[i].quantity + '</span><span>皮重:</span><span class="sp goods_cppz">' +
                        (data[i].tareQuantity ? data[i].tareQuantity : 0) + '</span></p></div>' +
                        '<div class="col-10"><span class="icon iconfont icon-msnui-jian pull-right"></span></div></div></li>';

                }
                $(".content .list-block .goodsList_ul").empty().html(data_html);
                $alert("获取成功", 800);
                badge_text();
            } else {
                $alert(res.message);
            }
        },
        error: function (error) {
            loading_hide();
            if (error.status == 0) {
                $alert("请求超时，请重新操作！");
            } else {
                $alert(error.responseText);
            }
        }
    });

    //根据扫描内容 得到 产品信息
    $("#goods_id").on("keydown", function (event) {
        if (event.keyCode == "13") {
            if (pass == 0) {
                return
            }
            pass = 0;
            var val = $(this).val();
            if (!val) {
                $alert("请扫描产品标签！");
                setTimeout(function () {
                    pass = 1;
                    $("#goods_id").val("").focus();
                }, 1400);
                return;
            }
            $(this).val("");
            loading_show("获取中...");
            $.ajax({
                headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
                type: "post",
                url: url + '/app/deliver/getRopeProductwarehouse',
                data: {
                    _id: deliverRope.id,
                    workplanNumber: deliverRope.workplanNumber,
                    batch: val
                },
                dataType: 'json',
                json: 'callback',
                cache: false,
                timeout: 60000,
                success: function (res) {
                    pass = 1;
                    loading_hide();
                    if (res.success) {
                        var datas = res.result;
                        for (var i = 0; i < datas.length; i++) {
                            if ($(".content .list-block .goodsList_ul li[attr-data='" + (datas[i].batch) + "']").length != 0) {
                                $alert("不可重复扫描");
                                setTimeout(function () {
                                    $("#goods_id").val("").focus();
                                }, 1400);
                                return
                            } else {
                                var data_html = '<li attr-tjh="' + (datas[i].palletBatch || "") + '" attr-data="' + datas[i].batch + '"><div class="row no-gutter">' +
                                    '<div class="col-90">' +
                                    '<p></p><p><span>编号:</span><span class="sp goods_cpph">' +
                                    datas[i].batch + '</span></p>';
                                if (datas[i].palletBatch) {
                                    data_html += '<p><span>托架号:</span><span class="sp goods_cptjh">' + (datas[i].palletBatch || "") + '</span></p>';
                                }
                                data_html += '<p><span>净重:</span><span class="sp goods_cpzl">' +
                                    datas[i].quantity + '</span><span>皮重:</span><span class="sp goods_cppz">' +
                                    (datas[i].tareQuantity ? datas[i].tareQuantity : 0) + '</span></p>' +
                                    '<p><span>段长标注:</span><span class="sp goods_dcbz">' + (datas[i].segmentLength || "") + '</span></p></div>' +
                                    '<div class="col-10"><span class="icon iconfont icon-msnui-jian pull-right"></span></div></div></li>';
                                $(".content .list-block .goodsList_ul").prepend(data_html);
                            }
                        }
                        $alert(val + "获取成功", 800);
                        badge_text();
                        setTimeout(function () {
                            $("#goods_id").val("").focus();
                        }, 1400);
                    } else {
                        $alert(res.message);
                    }
                },
                error: function (error) {
                    pass = 1;
                    loading_hide();
                    console.log(error);
                    if (error.status == 0) {
                        //loading_hide();
                        $alert("请求超时，请重新操作！");
                    } else {
                        $alert(error.responseText);
                    }
                    setTimeout(function () {
                        $("#goods_id").val("").focus();
                    }, 1400);
                }
            });
        }
    });

    // li 中的 减号 点击 删除 本条
    $(".content").on("click", ".goodsList_ul li .col-10", function () {
        var jianhao = $(this);
        $.confirm('确定删除此货品？', function () {
            var cptjh = jianhao.parent().parent().find("span.goods_cptjh").text();
            if (cptjh) {
                $(".content .list-block .goodsList_ul li[attr-tjh='" + cptjh + "']").remove();
            } else {
                jianhao.parent().parent().remove();
            }
            badge_text();
            $alert("删除成功");
            $("#goods_id").val("").focus();
        });
    });

    //点击 保存数据
    $(".foot").on("click", ".add_success", function () {
        if ($(".content .goodsList_ul li").length <= 0) {
            $("#goods_id").val("").focus();
            return $alert("请扫描发货产品！");
        }
        $.confirm("确认保存数据吗？", function () {
            if (pass == 0) {
                return
            }
            pass = 0;
            var batchs = [];
            for (var i = 0; i < $(".content .goodsList_ul li").length; i++) {
                batchs.push($(".content .goodsList_ul li").eq(i).find("span.goods_cpph").text());
            }
            loading_show("保存中...");
            //获取 货品列表中的信息
            $.ajax({
                headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
                type: "POST",
                url: url + "/app/deliver/preservationRopeSaleoutorder",
                data: {
                    _id: deliverRope.id,
                    //workplanNumber: deliverRope.workplanNumber,
                    batchs: JSON.stringify(batchs)
                },
                timeout: 60000,
                cache: false,
                success: function (res) {
                    pass = 1;
                    loading_hide();
                    if (res.success) {
                        console.log("销售发货保存成功");
                        localStorage.removeItem("deliverRope");
                        window.location.href = "deliverRope_one.html";
                    } else {
                        $alert(res.message)
                    }
                },
                error: function (error) {
                    pass = 1;
                    if (error.status == 0) {
                        loading_hide();
                        $alert("请求超时，请重新操作！");
                    } else {
                        loading_hide();
                        $alert(error.responseText);
                    }
                }
            });
        });
    });

    /*// 点击 确认发货 信息 传给后台
    $(document).on("click", ".foot .add_success", function () {
        if ($(".content .goodsList_ul li").length <= 0) {
            $("#goods_id").val("").focus();
            return $alert("请扫描发货产品！");
        }
        $.confirm('确定发货？', function () {
            if (pass == 0) {
                return
            }
            pass = 0;
            var numbers = [];
            for (var i = 0; i < $(".content .goodsList_ul li").length; i++) {
                numbers.push($(".content .goodsList_ul li").eq(i).find("span.goods_cpph").text());
            }
            loading_show("发货中...");
            //获取 货品列表中的信息
            $.ajax({
                headers: {
                    session: getcookie("session")
                },
                type: "POST",
                url: url + "/app/api/storekeeper/saveSaleoutorder",
                data: {
                    _id: deliverRope.id,
                    workplanNumber: deliverRope.workplanNumber,
                    numbers: JSON.stringify(numbers)
                },
                timeout: 60000,
                cache: false,
                success: function () {
                    pass = 1;
                    loading_hide();
                    console.log("销售发货成功");
                    localStorage.removeItem("deliverRope");
                    window.location.href = "deliverRope.html";
                },
                error: function (error) {
                    pass = 1;
                    if (error.status == 0) {
                        loading_hide();
                        $alert("请求超时，请重新操作！");
                    } else {
                        loading_hide();
                        $alert(error.responseText);
                    }
                }
            });
        });
    });*/

    //点击 某一条 数据  改变 背景颜色
    $(document).on("click", ".content .goodsList_ul li", function () {
        $(this).css("background", "#cccccc").siblings("li").css("background", "#ffffff");
    });

    // goodsList中 badge 的值 为 列表中 li 的个数
    function badge_text() {
        var badge_len = $(".content .goodsList_ul li").length;
        //console.log(badge_len);
        var allWeight = 0;
        for (var i = 0; i < badge_len; i++) {
            var weight = $(".content .goodsList_ul li").eq(i).find(".row span.goods_cpzl").html();
            allWeight = accAdd(allWeight, weight);
        }
        $(".details span.good_length").html(badge_len);
        $(".details span.good_weight").html(allWeight);
    }

    badge_text();

});